import urllib
import vlc
from utils import *

def make_uri(filename):
    uri = urlparse.urlunsplit(('file', os.path.join(mount_path, "Media", filename), '', '', ''))
    return uri


class VideoPlayer(object):
    """
    State variables which affect how a video is played back.
    """
    # Which streams to play
    audio = 0           # Default is to play first audio track in stream
    video = 0           # Default is to play first video track in stream
    text  = None        # Default is no subtitles

    # Screen size and shape
    pixel_aspect = 1.0          # Square pixels are default
    display_width = 1920        # Defaults to HD1080
    display_height = 1080
    
    def get_display_aspect(self):
      return self.display_width * self.pixel_aspect / self.display_height
    display_aspect = property(get_display_aspect)

    # Markers
    bookmark = 0.0      # Seconds. Default is beginning of stream file.
    
    # Lib-Ray disk interface controls:
    names = ['audio', 'text', 'video', 'chapter', 'bookmark']
    
    def __init__(self, config):
        """
        Load initial settings from the provided ConfigParser object.
        """
        audio = lazytype(config.get('LibRayVideo', 'audio'), multi=True)
        if audio: self.audio = audio
        
        text = lazytype(config.get('LibRayVideo', 'subtitle'), multi=True)
        if text: self.text = text
         
        video = lazytype(config.get('LibRayVideo', 'video'), multi=True)
        if video: self.video = video
        
        pixel_aspect = lazytype(config.get('LibRayVideo', 'pixel_aspect'))
        if pixel_aspect: self.pixel_aspect = pixel_aspect
        
        display_width = lazytype(config.get('LibRayVideo', 'display_width'))
        display_height = lazytype(config.get('LibRayVideo', 'display_height'))
        display_aspect = lazytype(config.get('LibRayVideo', 'display_aspect'))
        
        # Any two and compute the third
        if display_width and display_height:
            self.display_width = display_width
            self.display_height = display_height
        elif display_width and display_aspect:
            self.display_width = display_width
            self.display_height = int(display_width/display_aspect)
        elif display_height and display_aspect:
            self.display_height = display_height
            self.display_width = int(display_height*display_aspect)
        else:
            # Only one value specified -- others are defaulted
            if display_width: self.display_width = display_width
            if display_height: self.display_height = display_height
            if display_aspect: self.display_height = int(self.display_width/display_aspect)
        
        bookmark = lazytype(config.get('LibRayVideo', 'bookmark'))
        if bookmark: self.bookmark = bookmark
        
        # Create a VLC instance for the video player
        self.vlc = vlc.Instance()
        
    def load_video(self, videofilename):
        """
        Register the video file and collect profile data.
        """
        self._player = self.vlc.media_player_new(make_uri(videofilename))
        self.chapters = {}
        for i, key in self._player.video_get_chapter_description(0):
            self.chapters[key] = i
            
        # Probably unnecessary - resets to beginning:
        player.set_chapter(0)
        player.stop()
           
#class Video(object):
#    def __init__(self, player):
#        self._vlc_player = player.vlc
        
    def play(self):
        """
        Begin playback.
        """
        self._vlc_player.set_fullscreen(False)
        self._vlc_player.set_fullscreen(True)
        self._vlc_player.play()
        if self.chapter:
            self._vlc_player.set_chapter(self.chapters[self.chapter])


    def set_audio(self, code):
        """
        Set which audio track will be played back.
        Value requires validation.
        """
        raise NotImplemented
        
    def set_text(self, code):
        """
        Set which text track (usually subtitles) to be played back.
        Value requires validation.
        """
        raise NotImplemented
    
    def set_video(self, code):
        """
        Set which video track (alternate views or "angles") to be played back.
        Value requires validation.
        """
        raise NotImplemented
        
    def set_chapter(self, code):
        """
        Set chapter at which to start playback.
        Value can be an integer from 1 to the number of chapters in the file,
        or it can be a string matching the chapter names in the file (to be
        compliant, they must follow "identifier" rules -- must begin with
        a letter or an underscore with the rest made up of letters, numbers,
        or underscore up to 32 characters. No blanks, dashes, dots, or other
        special characters). This is partly to avoid snafus with the GET
        variable-passing from the volume.
        """
        raise NotImplemented
        
    def set_bookmark(self, code):
        """
        Frame number to begin playback from beginning of video file, without
        reference to chapters.
        """
        raise NotImplemented
        
